package com.dtjd.playcoinmonkey.activities;

import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.dtjd.playcoinmonkey.MyApplication;
import com.dtjd.playcoinmonkey.R;
import com.dtjd.playcoinmonkey.bean.HttpParaBean;
import com.dtjd.playcoinmonkey.databinding.ActivitySignBinding;
import com.dtjd.playcoinmonkey.utils.Network;
import com.dtjd.playcoinmonkey.utils.Tools;

import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Objects;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class SignActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivitySignBinding binding = ActivitySignBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setTitle(R.string.sign);
        ActionBar actionbar = getSupportActionBar();
        if (actionbar!=null){
            actionbar.setDisplayHomeAsUpEnabled(true);
            actionbar.setHomeButtonEnabled(true);
        }
        setResult(6);
        binding.signSign.setOnClickListener(view -> {
            if (!binding.signCb.isChecked()){
                Tools.toast(getResources().getString(R.string.toastclause));
            }else {
                String email = binding.signEmail.getText().toString();
                String password = binding.signPassword.getText().toString();
                String password2 = binding.signPassword2.getText().toString();
                if (email.equals("")){
                    Tools.toast( getResources().getString(R.string.input_email));
                    return;
                }
                if (!Tools.isEmail(email)){
                    Tools.toast( getResources().getString(R.string.emailFormatError));
                    return;
                }
                if (password.equals("")){
                    Tools.toast( getResources().getString(R.string.input_password));
                    return;
                }
                if (password2.equals("")){
                    Tools.toast( getResources().getString(R.string.input_password2));
                    return;
                }
                if (password2.length()<8){
                    Tools.toast( getResources().getString(R.string.password_illegal));
                    return;
                }
                if (!password.equals(password2)){
                    Tools.toast(getResources().getString(R.string.password_not_match) );
                    return;
                }
                if (!Tools.passwordCorrect(password)){
                    Tools.toast(getResources().getString(R.string.password_illegal) );
                    return;
                }
                ArrayList<HttpParaBean> list=new ArrayList<>();
                list.add(new HttpParaBean("account",email));
                list.add(new HttpParaBean("password",password));
                Network.post(list, "wbh/register", new Callback() {
                    @Override
                    public void onFailure(@NonNull Call call, @NonNull IOException e) {
                        e.printStackTrace();
                    }
                    @Override
                    public void onResponse(@NonNull Call call, @NonNull Response response) {
                        runOnUiThread(() -> {
                            try {
                                JSONObject jo = new JSONObject(response.body().string());
                                if (jo.getInt("code")==2){
                                    ArrayList<HttpParaBean> list=new ArrayList<>();
                                    list.add(new HttpParaBean("phone",email));
                                    list.add(new HttpParaBean("password",password));
                                    Network.post(list, "wbh/wbhLogin", new Callback() {
                                        @Override
                                        public void onFailure(@NonNull Call call, @NonNull IOException e) {
                                            e.printStackTrace();
                                        }
                                        @Override
                                        public void onResponse(@NonNull Call call, @NonNull Response response) {
                                            runOnUiThread(() -> {
                                                try {
                                                    JSONObject jo = new JSONObject(Objects.requireNonNull(response.body()).string());
                                                    if (jo.getInt("code")==1){
                                                        @SuppressLint("CommitPrefEdits") SharedPreferences.Editor edit = MyApplication.sp.edit();
                                                        edit.putString("token",jo.getJSONArray("datalist").getJSONObject(0).getString("token"));
                                                        edit.apply();
                                                        setResult(7);
                                                        finish();
                                                    }else {
                                                        setResult(7);
                                                        finish();
                                                    }
                                                } catch (Exception e) {
                                                    e.printStackTrace();
                                                }
                                            });
                                        }
                                    });
                                }else {
                                    Tools.toast(jo.getString("datalist"));
                                }
                            } catch (Exception e){
                                e.printStackTrace();
                            }
                        });
                    }
                });
            }
        });
    }
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (android.R.id.home==item.getItemId()){ finish(); }
        return super.onOptionsItemSelected(item);
    }
}